package com.esms.struts.service;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.esms.bean.Group;
import com.esms.bean.Person;
import com.esms.bean.PersonDAO;

public class PersonService {
	
	private static Logger log = Logger.getLogger(PersonService.class);
	
	private PersonService() {}
	public static PersonService $ = new PersonService(); 
	
	public void createNewPerson(Person person) {
		PersonDAO.$.insert(person);
	}
	
	public List getPersons(int personId) {
		return PersonDAO.$.findByCreator(personId);
	}
	
	public List getPersons(String userid) {
		List list = PersonDAO.$.findByUserid(userid);
		int personId = (list.size() == 0) ? -1 : ((Person) list.get(0)).getId(); 
		log.info("personId >> " + personId); 
		return getPersons(personId);
	}
	
	public int getPersonId(String userid) {
		List list = PersonDAO.$.findByUserid(userid);
		Person person = list.isEmpty() ? null : (Person) list.get(0);
		return null == person ? -1 : person.getId();
	}
	
	public Person getPerson(String userid) {
		List list = PersonDAO.$.findByUserid(userid);
		Person person = list.isEmpty() ? null : (Person) list.get(0);
		return person; 
	}
	
	public String getPersonType(Person person) {
		Set personType = person.getGroups(); 
		if(personType.isEmpty())
			return "�޷���";
		else {
			Iterator it = (Iterator) personType.iterator();
			Group group =  (Group) it.next();
			return group.getGroupname();
		}
	}
	
	public void deletePerson(Person person) {
		PersonDAO.$.delete(person);
	}
	
	public Person getCreatePerson(Person person) {
		Person creator = PersonDAO.$.findById(person.getCreator());
		return creator;
	}
}
